# -*- coding: utf-8 -*-
from jms.aigroup.ai_dwd.train_sample_daily_bak import jms_ai_dwd__train_sample_bak
from jms.aigroup.ai_dwd.yl_ml_clean_address_new_day import jms_ai_dwd__yl_ml_clean_address_new_day
from jms.aigroup.ai_group.geo_info_day import jms_aigroup__geo_info_day
from jms.ods.tab.tab_barscan_collect import jms_ods__tab_barscan_collect
from utils.operators.latest_only_spark_submit_operator import LatestOnlySparkSubmitOperator
from utils.alerts.yl_threeSegCodeOnFailue import yl_threeSegCodeOnFailure
from utils.alerts.yl_threeSegCodeOnSuccess import yl_threeSegCodeOnSuccess

def kwargs():
    kwargs = {
        "db": "ai_group",
        "table": "train_sample",
        "desc": "模型训练经纬度地址数据",
        "taskid": "10060",
        "ifprivacy": 0,
        "warnignore": 0,
    }
    return kwargs

jms_ai_group__train_sample = LatestOnlySparkSubmitOperator(
    task_id='jms_ai_group__train_sample',
    conn_id='spark_default',
    pool_slots=10,
    # depends_on_past=True,  # 如果任务依赖于前一天的同名任务，则将 depends_on_past 设为 True
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_ai_group__train_sample_{{ execution_date| date_add(1) | cst_ds }}',  # yarn 任务名称
    yarn_queue='pyspark',
    driver_memory='4G',
    driver_cores=2,
    executor_memory='10G',
    executor_cores=4,
    num_executors=90,
    depends_on_past=False,
    email=['jarl.huang@jtexpress.com','yl_bigdata@yl-scm.com'],
    conf={'spark.core.connection.ack.wait.timeout': 300,
          'spark.locality.wait': 60,
          'spark.dynamicAllocation.maxExecutors': 100,
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 40,
          'spark.dynamicAllocation.enabled': 'true',
          'spark.shuffle.service.enabled': 'true',  # NodeManager中一个长期运行的辅助服务，用于提升Shuffle计算性能
          'spark.executor.memoryOverhead': '4G',
          'spark.sql.shuffle.partitions': 1000,  # spark.sql.shuffle.partitions则是只对SparkSQL有效
          'spark.shuffle.memoryFraction': '0.8',
          'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=4'
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 100000,  #
              'hive.exec.max.dynamic.partitions.pernode': 1000,  #
              },
    java_class='com.yunlu.bigdata.jobs.ml.TrainSampleForML',  # spark 主类
    # application='hdfs:///user/spark/work/aigroup/train_sample/jobs-1.0-SNAPSHOT-jar-with-dependencies.jar',  # spark jar 包
    application='hdfs:///scheduler/jms/spark/sj/train_sample/jobs-1.0-SNAPSHOT-jar-with-dependencies.jar',
    # spark jar 包
    application_args=['{{ execution_date | cst_ds_nodash }}'],  # 参数dt 20201026
    on_success_callback=yl_threeSegCodeOnSuccess(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_info"),
    on_failure_callback=yl_threeSegCodeOnFailure(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_alert"),
)

# 设置依赖
jms_ai_group__train_sample << [
    jms_ai_dwd__yl_ml_clean_address_new_day,
    jms_aigroup__geo_info_day ,jms_ai_dwd__train_sample_bak,
    jms_ods__tab_barscan_collect]
